import 'package:cher_flare/utils/app_color.dart';
import 'package:cher_flare/utils/ly_use_log_manager.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';

class LYWebViewPage extends StatefulWidget {
  const LYWebViewPage({super.key});

  @override
  State<LYWebViewPage> createState() => _LYWebViewPageState();
}

class _LYWebViewPageState extends State<LYWebViewPage> {
  late WebViewController controller;
  String title = '';
  @override
  void initState() {
    super.initState();
    late final PlatformWebViewControllerCreationParams params;
    if (WebViewPlatform.instance is WebKitWebViewPlatform) {
      params = WebKitWebViewControllerCreationParams(
        allowsInlineMediaPlayback: true,
        mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
      );
    } else {
      params = const PlatformWebViewControllerCreationParams();
    }
    controller = WebViewController.fromPlatformCreationParams(params);
    final urlString = Get.arguments['urlString'];
    title = Get.arguments['title'] ?? '';
    final type = Get.arguments['type'];
    if (type == 0) {
      LYUseLogManager.stat(page: LYLogPages.privacy);
    } else if (type == 1) {
      LYUseLogManager.stat(page: LYLogPages.terms);
    }
    final url = Uri.tryParse(urlString);
    if (url != null) {
      controller.loadRequest(url);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          title,
          style: const TextStyle(fontSize: 18, color: AppColors.color202020),
        ),
        backgroundColor: AppColors.colorD7FFDF,
        leading: InkWell(
          child: Image.asset(
            'assets/images/back_icon.png',
            width: 24,
            height: 24,
          ),
          onTap: () {
            Get.close(0);
          },
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: WebViewWidget(controller: controller),
    );
  }
}
